﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using UtilLib;

namespace UtilPublic.Redis
{
    /// <summary>
    /// Hash存储
    /// </summary>
    public partial class RedisLink
    {
        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HashSet<T1, T2, T3>(T1 hashId, T2 key, T3 value)
        {
            try
            {
                RedisDb.HashSet($"{hashId}", $"{key}", $"{value}");
                return true;
            }
            catch
            {
                loger.Fatal($"HashSet Error {hashId} {key} {value}");
                return false;
            }
        }
        /// <summary>
        /// 批量设置
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public bool HashSetDict<T1, T2, T3>(T1 hashId, Dictionary<T2, T3> dic)
        {
            try
            {
                foreach (var kvp in dic)
                {
                    RedisDb.HashSet($"{hashId}", $"{kvp.Key}", $"{kvp.Value}");
                }
                return true;
            }
            catch
            {
                loger.Fatal($"HashSetDict Error {hashId}");
                return false;
            }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string HashGet<T1, T2>(T1 hashId, T2 key)
        {
            string result = "";
            try
            {
                result= RedisDb.HashGet($"{hashId}", $"{key}");
                if (string.IsNullOrEmpty(result))
                {
                    result = "";
                }
            }
            catch
            {
                loger.Fatal($"HashGet Error {hashId} {key}");
            }
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool HashDelete<T1, T2>(T1 hashId, T2 key)
        {
            try
            {
                return RedisDb.HashDelete($"{hashId}", $"{key}");
            }
            catch
            {
                loger.Fatal($"HashDelete Error {hashId} {key}");
                return false;
            }
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="hashId"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool HashExists<T1, T2>(T1 hashId, T2 key)
        {
            try
            {
                return RedisDb.HashExists($"{hashId}", $"{key}");
            }
            catch
            {
                loger.Fatal($"HashExists Error {hashId} {key}");
                return false;
            }
        }
        /// <summary>
        /// 长度
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public long HashLength<T>(T hashId)
        {
            try
            {
                return RedisDb.HashLength($"{hashId}");
            }
            catch
            {
                loger.Fatal($"HashLength Error {hashId}");
                return 0;
            }
        }
        /// <summary>
        /// 获取全部Keys
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public RedisValue[] HashKeys<T>(T hashId)
        {
            try
            {
                return RedisDb.HashKeys($"{hashId}");
            }
            catch
            {
                loger.Fatal($"HashKeys Error {hashId}");
                return new RedisValue[0];
            }
        }
        /// <summary>
        /// 获取全部Hash 慎用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public HashEntry[] HashGetAll<T>(T hashId)
        {
            try
            {
                return RedisDb.HashGetAll($"{hashId}");
            }
            catch
            {
                loger.Fatal($"HashGetAll Error {hashId}");
                return new HashEntry[0];
            }
        }
        /// <summary>
        /// 获取全部Hash 慎用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hashId"></param>
        /// <returns></returns>
        public Dictionary<string, string> HashGetDict<T>(T hashId)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            var datas = HashGetAll(hashId);
            foreach (var kvp in datas)
            {
                result.Add(kvp.Name, kvp.Value);
            }
            return result;
        }

    }
}